class CiesController < ApplicationController
  # GET /cies
  # GET /cies.json

  def index
    @cies = Cy.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @cies }
    end
  end

  # GET /cies/1
  # GET /cies/1.json
  def show
    @cy = Cy.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @cy }
    end
  end

  # GET /cies/new
  # GET /cies/new.json
  def new
    @cy = Cy.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @cy }
    end
  end

  # GET /cies/1/edit
  def edit
    @cy = Cy.find(params[:id])
  end

  # POST /cies
  # POST /cies.json
  def create
    @cy = Cy.new(params[:cy])

    respond_to do |format|
      if @cy.save
        format.html { redirect_to @cy, notice: 'Cy was successfully created.' }
        format.json { render json: @cy, status: :created, location: @cy }
      else
        format.html { render action: "new" }
        format.json { render json: @cy.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /cies/1
  # PUT /cies/1.json
  def update
    @cy = Cy.find(params[:id])

    respond_to do |format|
      if @cy.update_attributes(params[:cy])
        format.html { redirect_to @cy, notice: 'Cy was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @cy.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cies/1
  # DELETE /cies/1.json
  def destroy
    @cy = Cy.find(params[:id])
    @cy.destroy

    respond_to do |format|
      format.html { redirect_to cies_url }
      format.json { head :ok }
    end
  end

    # POST /cies/1/receive_datagram
  # 接收报文
  def receive_datagram

    cy_datagram = ActiveSupport::JSON.decode params[:data]
    logger.debug cy_datagram
    cy = Cy.find_by_code("#{params[:id]}")
    cy.datagram_save(cy_datagram, params[:id])
    cy.read_datagram(cy_datagram)
    render :text => '200'
  end

  skip_before_filter :require_login, :only => :receive_datagram

end
